<template>
  <div>
    <h3>课程表</h3>
    <div>
      <el-button type="success" size='mini' @click="$router.push('/courses/create')">创建</el-button>
    </div>
    <el-table :data="data.data" size="mini" border stripe>
      <el-table-column
        v-for="(field,name) in fields"
        :prop="name"
        :key="name"
        :label="field.label"
        :width="field.width"
      ></el-table-column>
      <el-table-column label="操作" width="200">
        <template v-slot="{row}">
          <el-button type="success" size="mini" @click="$router.push(`/courses/edit/${row._id}`)">编辑</el-button>
          <el-button type="primary" size="mini" @click="deleteCourses(row._id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script lang="ts">
import { Vue, Component } from 'vue-property-decorator';

@Component({})
export default class extends Vue {
  data = {};
  fields = {
    _id: { label: 'ID' },
    name: { label: '课程名称' },
    cover: { label: '课程封面' },
  };
  async fetch() {
    const { data } = await this.$http.get('course');
    this.data = data;
  }

  async deleteCourses(id:string) {
    await this.$http.delete(`course/${id}`);
    this.fetch();
    this.$message.success('成功');
  }
  created() {
    this.fetch();
  }
}
</script>